package com.abaaba.project2.controller;

import com.abaaba.project2.bean.Order;
import com.abaaba.project2.bean.bo.AdminCommentReplyBO;
import com.abaaba.project2.bean.bo.OrderRefundBo;
import com.abaaba.project2.bean.bo.OrderShipBo;
import com.abaaba.project2.bean.data.BaseData;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.bean.vo.DetailVo;
import com.abaaba.project2.service.AdminCommentService;
import com.abaaba.project2.service.AdminOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    AdminOrderService adminOrderService;

    @Autowired
    AdminCommentService adminCommentService;

    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, Short[] orderStatusArray, String sort, String order, Integer userId, String orderSn) {
        BaseData<Order> orderBaseData = adminOrderService.queryOrder(page, limit, orderStatusArray, sort, order, userId, orderSn);
        return BaseRespVo.ok(orderBaseData);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        DetailVo detailVo = adminOrderService.queryDetail(id);
        return BaseRespVo.ok(detailVo);
    }

    @RequestMapping("ship")
    public BaseRespVo ship(@RequestBody OrderShipBo orderShipBo) {
        adminOrderService.shipOrderById(orderShipBo);
        return BaseRespVo.ok();
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody OrderRefundBo orderRefundBo) {
        try {
            adminOrderService.refundOrderById(orderRefundBo);
        }catch (Exception e){
            return BaseRespVo.fail(621,"订单退款失败");
        }

        return BaseRespVo.ok();
    }

    @RequestMapping("reply")
    public BaseRespVo reply(@RequestBody AdminCommentReplyBO adminCommentReplyBO) {

        int code = adminCommentService.CommentReply(adminCommentReplyBO);
        if (adminCommentReplyBO.getContent() ==""){
            return BaseRespVo.fail("参数不能为空");
        }
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(code);
        baseRespVo.setData("成功");
        baseRespVo.setErrmsg("添加成功");
        return BaseRespVo.ok();
    }

}
